<?php

namespace app\admin\controller;

use app\common\model\ShopMemberCard;
use app\common\model\ShopStore;
use app\common\service\Status;
use buildView\Actions;
use buildView\Filter;
use think\Controller;
use buildView\Grid;
use app\common\model\Account as ShopAccountLog;
use app\common\model\ShopSchoolRegion;

/**
 * 账单记录
 * Class Account
 * @package app\admin\controller
 */
class Account extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $store = session('store');
        $grid = new Grid(new ShopAccountLog);
        if ($store){
            $grid->model()->where('store_id',$store['id']);
        }
        $region_user = session('admin_user');
        if($region_user['store_id']==0 || $region_user['store_id']==null and $region_user['region_id'] > 0 || $region_user['region_id'] != null){
            // 区分市、区代理
            $select_region = ShopSchoolRegion::where('id',$region_user['region_id'])->find();
            if($select_region['pid'] == 0){
                // 市代理
                $area_region = ShopSchoolRegion::where('pid',$select_region['id'])->select();
                $raea_ids = [];
                foreach ($area_region as $key=>$value){
                    $raea_ids[$key] = $value['id'];
                }
                
                $region_storeId = ShopStore::whereIn('region_id',$raea_ids)->select();
                $storeId_ids=[];
                foreach ($region_storeId as $key=>$value){
                    $storeId_ids[$key] = $value['id'];
                }
                $grid->model()->whereIn('store_id',$storeId_ids);
                
            }else{
                // 区代理
                $region_storeId = ShopStore::whereIn('region_id',$select_region['id'])->select();
                $storeId_ids=[];
                foreach ($region_storeId as $key=>$value){
                    $storeId_ids[$key] = $value['id'];
                }
                $grid->model()->whereIn('store_id',$storeId_ids);
            }
        }

        $grid->setTitle('账单记录');
        $grid->user()->headimg('会员')->display(function ($val, $data, $html) {
            return $html . '<br>' . $data['user']['nickname'];
        })->setExcelData(function($val,$data){
            return $data['user']['nickname'];
        })->image(50);
        if (!$store){
            $grid->store()->name('门店');
        }
        $grid->user()->phone('手机号');
        $grid->type('类型')->using(Status::ACCOUNTTYPE())->badge();
        $grid->order()->pay_type('支付方式')->using(Status::PAYTYPE());
        $grid->title('账单标题');
        $grid->order()->order_no('订单号')->setExcelData(function($val,$data){
            return ' '.$data['order']['order_no'];
        });
        $grid->create_at('账单时间');
        $grid->order()->buy_num('件数');
        $grid->order()->goods_price('单价');
        $grid->order()->discount_price('优惠券');
        $grid->order()->total_price('实收金额¥')->sort()->totalRow();
        $grid->order()->residue_num('剩余件数');
        $grid->memberExpire()->overdue_num('过期件数');

//        $grid->member('会员卡信息')->setExcelData(function ($vals,$data) use($store){
//            $html = '';
//            foreach ($vals as $val) {
//                $name = ShopMemberCard::where('status',1)->where('id',$val['card_id'])->value('name');
//                $has_num = $val['total_num'] - $val['use_num'];
//                $date = date('Y-m-d',$val['expire_time']);
//                if ($store){
//                    $html .= "{$name} 可使用总件数：{$val['total_num']}  剩余件数：{$has_num} 到期时间：{$date};";
//                }else{
//                    $store_name = ShopStore::where('id',$val['store_id'])->value('name');
//                    $html .= "{$store_name}  -- {$name} 可使用总件数：{$val['total_num']}  剩余件数：{$has_num} 到期时间：{$date};";
//                }
//            }
//            return $html;
//        })->hide();
//        $grid->total_has_num('已过期件数')->setExcelData(function ($vals,$data){
//            return $data['total_has_num'];
//        })->hide();
        $grid->actions(function (Actions $actions) {
            $actions->hideDetail();
            $actions->hideEdit();
        });
        $grid->filter(function (Filter $filter) use($store) {
            if (!$store) {
                $data = ShopStore::field('id,name')->column('name','id');
                $filter->eq('store_id', '门店')->select($data);
            }
            $filter->like( 'user.nickname','会员');
            $filter->like('user.phone', '手机号');
            $filter->like( 'title','账单标题');
            $type = Status::ACCOUNTTYPE();
            $filter->eq('order.pay_type', '支付方式')->select(Status::PAYTYPE());
            $filter->eq( 'type','账单类型')->select($type);
            $filter->dateBetween( 'create_at','账单时间');
        });
        $grid->hideAction();
        $grid->hideDeletesButton();
        $grid->hideAddButton();
        return $grid->view();
    }
}
